package org.twdata.pipeline.stage;

import org.twdata.pipeline.*;

// SAX imports:
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;

/**
 *  This convenience class implements all Generator methods to make it easier
 *  to create generators. 
 */
public abstract class AbstractGenerator implements Generator {

    /**  The <code>ContentHandler</code> receiving SAX events. */
    protected ContentHandler contentHandler;

    /**  The <code>LexicalHandler</code> receiving SAX events. */
    protected LexicalHandler lexicalHandler;
    
    /**  A unique id for this stage */
    protected String id;


    /**
     *  Gets the id
     *
     *@return    The id value
     */
     public String getId() {
        return id;   
     }
    
    /**
     *  Sets the id
     *
     *@param  id  The unique id
     */
     public void setId(String id) {
         this.id = id;
     }


    /**
     *  Set the <code>ContentHandler</code> that will receive XML data. <br>
     *  Subclasses may retrieve this <code>ContentHandler</code> instance
     *  accessing the protected <code>super.contentHandler</code> field.
     *
     *@param  consumer               The new consumer
     *@exception  PipelineException  If anything goes wrong
     */
    public void setConsumer(XMLConsumer consumer)
        throws PipelineException {
        this.contentHandler = consumer;
        this.lexicalHandler = consumer;
    }

    /**
     *  Sets the content handler
     *
     * @param handler The content handler
     */ 
    public void setContentHandler(ContentHandler handler) {
        this.contentHandler = handler;
    }

    /**
     *  Sets the lexical handler
     *
     * @param handler The lexical handler
     */
    public void setLexicalHandler(LexicalHandler handler) {
        this.lexicalHandler = handler;
    }    


    /**
     *  Sets up the instance for a new pipeline
     *
     *@param  ctx                    The current context
     *@exception  PipelineException  If anything goes wrong
     */
    public void setup(PipelineContext ctx)
        throws PipelineException { }


    /**  Recycles the instance */
    public void recycle() {
        contentHandler = null;
        lexicalHandler = null;
    }


    /**
     *  Generates SAX events
     */
    public abstract void generate() throws PipelineException;

}
